home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / trace / tcpdump-2.2.1 / print-ether.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-07  |  3.4 KB  |  124 lines

  1. /*
  2.  * Copyright (c) 1988-1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21. #ifndef lint
  22. static char rcsid[] =
  23.     "@(#) $Header: print-ether.c,v 1.22 91/10/07 20:18:28 leres Exp $ (LBL)";
  24. #endif
  25.  
  26. #include <sys/param.h>
  27. #include <sys/types.h>
  28. #include <sys/socket.h>
  29. #include <net/if.h>
  30. #include <netinet/in.h>
  31. #include <netinet/if_ether.h>
  32. #include <netinet/in_systm.h>
  33. #include <netinet/ip.h>
  34. #include <netinet/ip_var.h>
  35. #include <netinet/udp.h>
  36. #include <netinet/udp_var.h>
  37. #include <netinet/tcp.h>
  38. #include <netinet/tcpip.h>
  39.  
  40. #include "interface.h"
  41. #include "addrtoname.h"
  42.  
  43. u_char *packetp;
  44. u_char *snapend;
  45.  
  46. static inline void
  47. ether_print(ep, length)
  48.     register struct ether_header *ep;
  49.     int length;
  50. {
  51.     if (qflag)
  52.         (void)printf("%s %s %d: ",
  53.                  etheraddr_string(ESRC(ep)),
  54.                  etheraddr_string(EDST(ep)),
  55.                  length);
  56.     else
  57.         (void)printf("%s %s %s %d: ",
  58.                  etheraddr_string(ESRC(ep)),
  59.                  etheraddr_string(EDST(ep)),
  60.                  etherproto_string(ep->ether_type), 
  61.                  length);
  62. }
  63.  
  64. /*
  65.  * This is the top level routine of the printer.  'p' is the points
  66.  * to the ether header of the packet, 'tvp' is the timestamp, 
  67.  * 'length' is the length of the packet off the wire, and 'caplen'
  68.  * is the number of bytes actually captured.
  69.  */
  70. void
  71. ether_if_print(p, tvp, length, caplen)
  72.     u_char *p;
  73.     struct timeval *tvp;
  74.     int length;
  75.     int caplen;
  76. {
  77.     struct ether_header *ep;
  78.     register int i;
  79.  
  80.     ts_print(tvp);
  81.  
  82.     if (caplen < sizeof(struct ether_header)) {
  83.         printf("[|ether]");
  84.         goto out;
  85.     }
  86.  
  87.     if (eflag)
  88.         ether_print((struct ether_header *)p, length);
  89.  
  90.     /*
  91.      * Some printers want to get back at the ethernet addresses,
  92.      * and/or check that they're not walking off the end of the packet.
  93.      * Rather than pass them all the way down, we set these globals.
  94.      */
  95.     packetp = p;
  96.     snapend = p + caplen;
  97.     
  98.     length -= sizeof(struct ether_header);
  99.     ep = (struct ether_header *)p;
  100.     p += sizeof(struct ether_header);
  101.     switch (ntohs(ep->ether_type)) {
  102.  
  103.     case ETHERTYPE_IP:
  104.         ip_print((struct ip *)p, length);
  105.         break;
  106.  
  107.     case ETHERTYPE_ARP:
  108.     case ETHERTYPE_REVARP:
  109.         arp_print((struct ether_arp *)p, length, caplen - sizeof(*ep));
  110.         break;
  111.  
  112.     default:
  113.         if (!eflag)
  114.             ether_print(ep, length);
  115.         if (!xflag && !qflag)
  116.             default_print((u_short *)p, caplen - sizeof(*ep));
  117.         break;
  118.     }
  119.     if (xflag)
  120.         default_print((u_short *)p, caplen - sizeof(*ep));
  121.  out:
  122.     putchar('\n');
  123. }
  124.